﻿using Joker.Model;
using Joker.View.Editor;
using Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Joker.View.Management {
    public partial class SupplyManage : Form {
        private string sqlStr = " SELECT * FROM [Supply]";
        public SupplyManage() {
            InitializeComponent();
        }

        private void SypplyManage_Load(object sender, EventArgs e) {
            dataGridView1.DataSource = DBHelper.GetDataTable(sqlStr);
        }

        private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e) {
            if (e.RowIndex > 0 && e.RowIndex < dataGridView1.Rows.Count) dataGridView1.CurrentCell = dataGridView1.Rows[e.RowIndex].Cells[0];
        }

        private void button1_Click(object sender, EventArgs e) {
            dataGridView1.DataSource = DBHelper.GetDataTable($"{sqlStr} where [Name] like '%{textBox1.Text}%' and [Gender] = {(comboBox1.SelectedItem.Equals("男") ? "1" : "0")} and [Tel] like '%{textBox2.Text}%'");
        }

        private void 新增ToolStripMenuItem_Click(object sender, EventArgs e) {
            OpenForm.NewForm(this,new SupplyDataEdit(),false);
            dataGridView1.DataSource = DBHelper.GetDataTable(sqlStr);
        }

        private void 修改ToolStripMenuItem_Click(object sender, EventArgs e) {
            SupplyDataEdit dataEdit = new SupplyDataEdit();
            dataEdit.SupplyID = (int)dataGridView1.CurrentRow.Cells[0].Value;
            OpenForm.NewForm(this, dataEdit, false);
            dataGridView1.DataSource = DBHelper.GetDataTable(sqlStr);
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e) {
            if (MessageBox.Show($"确认删除ID为{dataGridView1.CurrentRow.Cells[0].Value}的供应商?","警告",MessageBoxButtons.YesNo,MessageBoxIcon.Warning) == DialogResult.Yes) {
                string del = $"delete [Supply] where [ID] = {dataGridView1.CurrentRow.Cells[0].Value}";
                if (DBHelper.ExecuteNonQuery(del)) {
                    MessageBox.Show("删除成功!");
                } else {
                    MessageBox.Show("删除失败!");
                }
            }
            dataGridView1.DataSource = DBHelper.GetDataTable(sqlStr);
        }
    }
}
